package com.example.orderassembly.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.orderassembly.mapper.ProductOrderDetailMapper;
import com.example.orderassembly.service.impl.ProductOrderDetailServiceImpl;
import com.woniu.cloud.common.exception.TicketException;
import com.woniuxy.api.ProductOrderDetailApi;
import com.woniu.cloud.common.dto.Result;
import com.woniuxy.entity.ProductOrderDetail;
import com.woniuxy.form.InsertGoodOrderDetail;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品订单详情表 前端控制器
 * </p>
 *
 * @author yang
 * @since 2021-09-21
 */
@RestController
@Slf4j
@SuppressWarnings("all")
public class ProductOrderDetailController implements ProductOrderDetailApi {
    @Resource
    ProductOrderDetailServiceImpl productOrderDetailService;

    @Override
    public Result getProductOrderDetailByUserId(Integer indexPage, Integer goodOrderId) {
        log.info("根据用户Id 获取其商品详情");
        return null;
    }

    @Override
    public Result getProductOrderDetailByStore(Integer storeId, Integer indexPage, Integer goodOrderId) {
        return null;
    }

    @Override
    public Result getProductOrderDetailByPlatform(Integer indexPage, Integer goodOrderId) {
        return null;
    }

    /**
     * 已测
     * @param insertGoodOrderDetail
     * @return
     */
    @PostMapping("/insertProductOrderDetail")
    @Override
    @GlobalTransactional
    public Result insertProductOrderDetail(InsertGoodOrderDetail insertGoodOrderDetail) {
        log.info("进入到新增商品订单详情 接口中 参数  insertGoodOrderDetail={}",insertGoodOrderDetail);
        if (insertGoodOrderDetail==null){
            log.info("认证错误");
//            return Result.fail("参数错误",null);
            throw  new TicketException("参数异常");
        }
        Boolean bool = productOrderDetailService.insertProductOrderDetail(insertGoodOrderDetail);
        return bool?Result.success("插入成功",bool):Result.fail("插入失败",bool);
    }

    /**
     * 已测
     * @param goodOrderId
     * @return
     */
    @GetMapping("/getProductOrderDetailByProductOrderId")
    @Override
    public Result getProductOrderDetailByProductOrderId(Integer goodOrderId){
        log.info("根据某个商品id 查询对应的 参数  商品订单id goodOrderId={}",goodOrderId);
        if (goodOrderId==null){
            log.info("认证错误");
//            return Result.fail("参数错误",null);
            throw  new TicketException("参数异常");
        }
        List<ProductOrderDetail> productOrderDetails = productOrderDetailService.getProductOrderDetailByProductOrderId(goodOrderId);

        return productOrderDetails.size()==0?Result.fail("查询失败",productOrderDetails):Result.success("查询成功",productOrderDetails);
    }
}

